// pages/regist/userredit.js
var utl = require('../../utils/util.js');
var city = require('../../utils/city.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ischecked: true,
    cantouch: false,
    showbank: false,
    banks: [],
    bindex: 0,
    citys: city.mtData().list,
    pindex: 0,
    cindex: -100,
    showp: false,
    showc: false,

    editcard: '', //身份证号码
    cardNum: '', //银行卡号

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.checkbank();
    if(app.globalData.userData){
      this.setData({
        uname: app.globalData.userData.userName,
        telnum: app.globalData.userData.loginName,
      })
    }
  },
  //查询银行
  checkbank: function() {
    utl.serverToken('/epay_apps/bankCard/searchBankListB0700', {}, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var rdata = res.response.bankList;
      this.setData({
        banks: rdata,
      })
    })
  },
  //提交
  registAction: function() {
    const isc = this.data.ischecked;
    const cardnum = this.data.cardNum;
    const edcard = this.data.editcard;
    const tel = this.data.telnum;
    var err = '';
    if (!isc) {
      wx.showToast({
        title: '同意服务协议才能注册',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if(cardnum == '' || edcard == '' || tel == ''){
      wx.showToast({
        title: '提交的信息不全',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    const bindex = this.data.bindex;
    const banks = this.data.banks;
    var bank = banks[bindex];
    var bank_jc = bank.value; //银行简称
    var bank_name = bank.text; //银行名称
    console.log('银行简称' + bank_jc);

    const pindex = this.data.pindex;
    const citys = this.data.citys;
    const cindex = this.data.cindex;
    var province = citys[pindex];
    var province_name = province.text; //省名称
    var province_code = province.value; //省CODE
    console.log('省会名称' + province_name);
    var provice_citys = province.children; //省辖下的市区
    var city_name = '';
    var city_code = '';
    if (province_name.search("市") != -1) {
      city_name = province_name;
      city_code = province_code;
    } else {
      var city = provice_citys[cindex];
      city_name = city.text;
      city_code = city.value;
    }
  wx.showLoading({
    title: '提交中...',
  })
    utl.serverToken('/epay_apps/user/updateUserInfoE0300', {
      'idNo': edcard,
      'name': this.data.uname,
      'mobile': tel,
      'accountNo': cardnum,
      'accountName':this.data.uname,
      'bankLeaveMobile':tel,
      'provinceName': province_name,
      'provinceId': province_code,
      'cityName': city_name,
      'cityId': city_code,
      'openBankName': bank_name,
      'bankCode': bank_jc,
      'issuerCodeName': bank_name,
      'issuerCode': '102290002756',
    }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      wx.showModal({
        title: '提示',
        content: '提交成功，请等待审核',
      })
    })
  },
//手机号
  committel:function(e){
    this.setData({
      telnum:e.detail.value,
    })
  },
  //绑定身份证号
  commitedit: function(e) {
    this.setData({
      editcard: e.detail.value,
    })
  },
  //银行卡号
  commitcard: function(e) {
    this.setData({
      cardNum: e.detail.value,
    })
  },
  //选择银行
  bindBankPickerChange: function(e) {
    this.setData({
      bindex: e.detail.value,
      showbank: true
    })
  },
  //绑定省
  bindProvincePickerChange: function(e) {
    this.setData({
      pindex: e.detail.value,
      showp: true,
      cindex: 0,
      cantouch: true
    })
  },
  //选择市区
  bindCityChange: function(e) {
    this.setData({
      cindex: e.detail.value,
      showc: true,

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  vonfire: function() {
    var check = this.data.ischecked;
    check = !check;
    this.setData({
      ischecked: check
    })
  },
})